function add(a, b) {
  try {
    a = String(a)
    b = String(b)
  }
  catch (err) {
    throw new Error('请输入数字或字符串')
  }

  let i = a.length
  let j = b.length

  let carry = 0
  const result = []

  while (i > 0 || j > 0 || carry > 0) {
    let sum = Number(a[i - 1] || 0) + Number(b[j - 1] || 0) + carry
    if (carry > 0)
      carry -= 1

    if (sum >= 10) {
      sum -= 10
      carry += 1
    }

    result.unshift(sum)
    i--
    j--
  }

  return Number(result.join(''))
}

// add('999', 2165)
export default add
